<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.username"
        placeholder="用户名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.email"
        placeholder="邮箱"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.phone"
        placeholder="手机号"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
    
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="100" align="center">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.avatar"
            :src="scope.row.avatar"
            style="width: 50px; height: 50px"
            :preview-src-list="[scope.row.avatar]"
          />
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.email || '未设置' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.phone || '未设置' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.last_login_time || '未登录' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登录IP" align="center" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.last_login_ip || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录次数" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.login_count || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >
            详情
          </el-button>
          <el-button
            :type="scope.row.status === 1 ? 'danger' : 'success'"
            size="mini"
            @click="handleModifyStatus(scope.row)"
          >
            {{ scope.row.status === 1 ? '禁用' : '启用' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 用户详情对话框 -->
    <el-dialog title="用户详情" :visible.sync="dialogVisible" width="600px">
      <el-form label-width="100px">
        <el-form-item label="用户ID">
          <span>{{ currentUser.id }}</span>
        </el-form-item>
        <el-form-item label="用户名">
          <span>{{ currentUser.username }}</span>
        </el-form-item>
        <el-form-item label="邮箱">
          <span>{{ currentUser.email || '未设置' }}</span>
        </el-form-item>
        <el-form-item label="手机号">
          <span>{{ currentUser.phone || '未设置' }}</span>
        </el-form-item>
        <el-form-item label="头像">
          <el-image
            v-if="currentUser.avatar"
            :src="currentUser.avatar"
            style="width: 100px; height: 100px"
            :preview-src-list="[currentUser.avatar]"
          />
          <span v-else>无</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-tag :type="currentUser.status === 1 ? 'success' : 'danger'">
            {{ currentUser.status === 1 ? '正常' : '禁用' }}
          </el-tag>
        </el-form-item>
        <el-form-item label="最后登录时间">
          <span>{{ currentUser.last_login_time || '未登录' }}</span>
        </el-form-item>
        <el-form-item label="最后登录IP">
          <span>{{ currentUser.last_login_ip || '-' }}</span>
        </el-form-item>
        <el-form-item label="登录次数">
          <span>{{ currentUser.login_count || 0 }}</span>
        </el-form-item>
        <el-form-item label="注册时间">
          <span>{{ currentUser.created_at }}</span>
        </el-form-item>
        <el-form-item label="更新时间">
          <span>{{ currentUser.updated_at }}</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button
          :type="currentUser.status === 1 ? 'danger' : 'success'"
          @click="handleModifyStatus(currentUser)"
        >
          {{ currentUser.status === 1 ? '禁用用户' : '启用用户' }}
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getH5UserList, updateH5UserStatus } from '@/api/h5User'

export default {
  name: 'H5UserList',
  components: { Pagination },
  // directives: { waves },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        username: undefined,
        email: undefined,
        phone: undefined,
        status: undefined
      },
      statusOptions: [
        { label: '正常', value: 1 },
        { label: '禁用', value: 0 }
      ],
      dialogVisible: false,
      currentUser: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 构建查询参数
      const params = {
        page: this.listQuery.page,
        limit: this.listQuery.limit
      }
      
      if (this.listQuery.username) {
        params.username = this.listQuery.username
      }
      
      if (this.listQuery.email) {
        params.email = this.listQuery.email
      }
      
      if (this.listQuery.phone) {
        params.phone = this.listQuery.phone
      }
      
      if (this.listQuery.status !== undefined) {
        params.status = this.listQuery.status
      }
      
      // 发起请求
      getH5UserList(params).then(response => {
        const { data, code, msg } = response
        if (code === 200) {
          this.list = data.list
          this.total = data.total
        } else {
          this.$message.error(msg || '获取用户列表失败')
        }
        this.listLoading = false
      }).catch(error => {
        console.error('获取用户列表失败:', error)
        this.$message.error('获取用户列表失败')
        this.listLoading = false
      })
    },
    
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    
    handleUpdate(row) {
      this.currentUser = Object.assign({}, row)
      this.dialogVisible = true
    },
    
    handleModifyStatus(row) {
      const status = row.status === 1 ? 0 : 1
      const statusText = status === 1 ? '启用' : '禁用'
      
      this.$confirm(`确定要${statusText}该用户吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateH5UserStatus({
          id: row.id,
          status
        }).then(response => {
          const { data } = response
          if (data.code === 200) {
            this.$message.success(`${statusText}成功`)
            // 更新列表中的状态
            if (this.dialogVisible && this.currentUser.id === row.id) {
              this.currentUser.status = status
            }
            // 更新列表
            this.getList()
          } else {
            this.$message.error(data.msg || `${statusText}失败`)
          }
        }).catch(error => {
          console.error(`${statusText}用户失败:`, error)
          this.$message.error(`${statusText}用户失败`)
        })
      }).catch(() => {
        // 取消操作
      })
    }
  }
}
</script>

<style scoped>
.filter-container {
  padding-bottom: 10px;
}
.filter-item {
  display: inline-block;
  vertical-align: middle;
  margin-bottom: 10px;
  margin-right: 10px;
}
</style> 